<template>
  <div class="user-info">
    <el-row :span="24">
      <el-col :span="8">
        <basic-container>
          <div class="user-info__content">
            <img class="user-info__img"
                 :src="userInfo.avatar"
                 alt="" />
            <p class="user-info__name">{{userInfo.name}}</p>
            <router-link class="user-info__setting"
                         :to="{path:'/info/setting'}">个人设置</router-link>
            <p class="user-info__desc">{{userInfo.loginId}}</p>
            <div class="user-info__detail-desc">
              <p><i class="el-icon-user"></i>
                <span title="用户姓名">{{userInfo.userName}}</span>
              </p>
              <p><i class="el-icon-phone"></i><span title="手机号">{{userInfo.telephone}}</span></p>
              <p><i class="el-icon-message"></i><span title="邮箱">{{userInfo.email}}</span></p>
              <p><i class="el-icon-postcard"></i><span title="所在部门">{{userInfo.orgName}}</span></p>
              <p><i class="el-icon-guide"></i><span title="邮编">{{userInfo.postCode}}</span></p>
              <p><i class="el-icon-location-information"></i><span title="地址">{{userInfo.address}}</span></p>
            </div>
            <div class="user-info__divider"></div>
            <h4>登录信息</h4>
            <div class="user-info__detail-desc">
              <p><i class="el-icon-time">最后登录时间：</i><span>{{userInfo.lastLoginTime}}</span></p>
              <p><i class="el-icon-collection-tag">登录次数：</i><span>{{userInfo.loginCount}}</span></p>
            </div>
          </div>
        </basic-container>
      </el-col>
      <el-col :span="16">
        <basic-container>
          <avue-form @tab-click="handleTabClick" :option="option" v-model="userInfo" @submit="handleSubmit">
          </avue-form>
        </basic-container>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import option from "@/const/user/info";
import store from '@/store/';
import { urllist } from '@/config/env';
import request from '@/router/axios';
import {
  resetPassword
} from '@/api/user';
export default {
  data () {
    return {
      type: 0,
      userInfo: store.getters.userInfo,
      option: option
    };
  },
  created () {
    if (this.$store.getters.isShowTenantCode){
      const org = this.findObject(this.option.group[0].column,'orgId');
      org.dicUrl = org.dicUrl + '?tenantCode=000000';
    }
  },
  methods: {
    handleSubmit (form,done) {
      if (this.type === 0){
        request({
          url: urllist.user + '/core/user/updateLogin',
          method: 'put',
          meta: {
            isSerialize: true
          },
          data: {
            id: form.userId,
            avatar: form.avatarBase,
            orgId: form.orgId,
            nickName: form.nickName,
            userName: form.userName,
            idType: form.idType,
            idNo: form.idNo,
            birthday: form.birthday,
            postCode: form.postCode,
            address: form.address,
          }
        }).then((data) => {
            if (data.data.status){
              form.avatar = urllist.download + form.avatarBase;
              form.orgName = form.$orgId;
              delete form.oldpassword;
              delete form.newpassword;
              delete form.newpasswords;

              this.$store.commit('SET_USERIFNO', form);
              this.$message({
                type: "success",
                message: "修改成功!"
              });
            }
            done();
        }).catch(() => {
          done();
        });
      }else if (this.type === 1) {
        if (form.newpassword != form.newpasswords){
          this.$message.error("新密码和确认密码不一致");
          done();
        }else{
          resetPassword({
            oldPw: window.md5(form.oldpassword).toUpperCase(),
            newPw: window.md5(form.newpassword).toUpperCase()
          }).then((data) => {
            if (data.data.status) {
              this.$store.commit('SET_PASSFLAG', true)
              this.$message.success('密码修改成功');
            }
            done();
          }).catch(() => {
            done();
          });
        }
      }
    },
    handleTabClick(tabs){
      this.type = Number(tabs.index);
      if (this.type === 1){
        for (let i = 0; i < 3; i++) {
          this.$set(this.option.group[1].column[i],'rules',[
            {
              required: true,
              message: '原密码必须填写'
            }
          ]);
        }
      }else {
        for (let i = 0; i < 3; i++) {
          this.$delete(this.option.group[1].column[i],'rules');
        }
      }

    }
  }
};
</script>

<style lang="scss">
.user-info {
  .avue-tabs {
    padding: 0 10px;
  }
  .el-tabs__content {
    padding: 20px 0;
  }
  &__img {
    display: block;
    margin: 0 auto;
    border-radius: 100%;
    width: 100px;
    height: 100px;
  }
  &__name {
    text-align: center;
    font-size: 20px;
    line-height: 28px;
    font-weight: 500;
    color: rgba(0, 0, 0, 0.85);
    margin-bottom: 0;
    margin-top: 10px;
  }
  &__setting {
    margin-bottom: 12px;
    display: block;
    font-size: 12px;
    color: #409eff;
    text-align: center;
  }
  &__desc {
    text-align: center;
    width: 200px;
    margin: 0 auto;
  }
  &__detail-desc {
    margin-top: 50px;
    font-size: 14px;
    p {
      margin-bottom: 10px;
    }
    span {
      margin-left: 10px;
    }
  }
  &__divider {
    border-top: 1px dashed #e8e8e8;
    display: block;
    height: 0;
    width: 100%;
    margin: 24px 0;
    clear: both;
  }
  &__tags {
    .el-tag {
      margin: 0 5px 5px 0;
    }
  }
}
</style>
